/*
 * @Author: 刘经标 2604132672@qq.com
 * @Date: 2025-04-12 17:52:56
 * @LastEditors: 刘经标 2604132672@qq.com
 * @LastEditTime: 2025-04-15 15:15:54
 * @FilePath: /ai-backend/src/routes/chat.routes.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { Router } from 'express';
import { chatHandler } from '../controllers/chat.controller.js';
import rateLimit from 'express-rate-limit';

const router = Router();

// 限流：15分钟内最多100次请求
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000,
  max: 100,
  message: '请求过于频繁，请稍后再试'
});

router.post('/chat', limiter, async (req, res) => {
  try {
    const response = await chatHandler(req, res);
    res.json(response);
  } catch (error) {
    if (error instanceof Error) {
      res.status(500).send(error.message);
    } else {
      res.status(500).send('An unexpected error occurred');
    }  }
});

export default router;